<template>
  <div class="blogs">
    <div class="content-header">
      <h1>个人中心<small>个人信息</small></h1>
      <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
        <el-breadcrumb-item >后台管理</el-breadcrumb-item>
        <el-breadcrumb-item>个人中心</el-breadcrumb-item>
        <el-breadcrumb-item>个人信息</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <!--中间内容-->
    <div class="app-container">
      <div class="box">
        <el-form :model="formData" :rules="userRules" ref="userForm" label-width="100px">
          <el-row>
            <el-col :span="8">
              <el-form-item prop="id" label="用户名">
                <el-input v-model="formData.id" :disabled="true"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="password" label="密码">
                <el-input v-model="formData.password" :disabled="true"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item prop="nick_name" label="昵称">
                <el-input v-model="formData.nick_name"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item prop="email" label="邮箱">
                <el-input v-model="formData.email"></el-input>
              </el-form-item>
            </el-col>
			<el-col :span="16">
			  <el-form-item prop="power" label="权限">
			    <el-input v-model="formData.power +'   (说明: 0无任何权限, 1可以发博客, 2拥有所有权限！)'" :disabled="true" ></el-input>
			  </el-form-item>
			</el-col>
          </el-row>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即修改</el-button>
<!--            <el-button @click="resetForm('formData')">重置</el-button>-->
          </el-form-item>
        </el-form>
      </div>
    </div>
    <br>
    <br>
  </div>
</template>

<script>
export default {
  data () {
    return {
		formData:null,
      userRules: {
        // 验证用户账号是否合法
		//因为这里的账号和密码不能更改，所以不用检查
        nick_name: [
          { required: true, message: '请输入新的昵称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入新邮箱地址', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在6 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
	//初始化方法 获得用户的 账号 密码 昵称 邮箱地址
    this.getUser()
  },
  methods: {
	//实现修改昵称与邮箱的方法
    submitForm (formName) {
      // 进行表单校验
	  var that=this
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          // 需要通过后台更改 nickName与邮箱
		  // console.log(that.formData)
		  axios.put("http://"+that.$store.state.host+":8181/user/updateUser",that.formData).then(function(res){
			  console.log(res)
			  if(res.data.status==true){
				  console.log("hahaha")
				//更新 sessionStorage中存储的用户信息 
				console.log(res.data.map.data)
				sessionStorage.setItem("user_info",JSON.stringify(res.data.map.data))  
				that.$message.success("修改成功!");  
			  }else{
				that.$message.error("修改失败!");  
			  }
		  })
        } else { // 校验不通过
          this.$message.error('校验失败，请检查输入格式!')
          return false
        }
      })
    },
	//这是置空
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    getUser () {
		//JSON.parse()将字符串转为json格式！！！！
        this.formData = JSON.parse(sessionStorage.getItem("user_info"))
    }
  }
}
</script>

<style>

</style>
